package com.zretc.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zretc.entity.Song;
import com.zretc.service.PlayListService;
import com.zretc.service.SongService;
import com.zretc.service.impl.PlayListServiceImpl;
import com.zretc.service.impl.SongServiceImpl;
import com.zretc.util.PageInfo;

/**
 * Servlet implementation class SongServlet
 */
@WebServlet("/SongServlet")
@MultipartConfig
public class SongServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private SongService songService = new SongServiceImpl();
    private PlayListService playListService = new PlayListServiceImpl();
    Gson gson = new Gson();
    
    public SongServlet() {
        super();
    }
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op = request.getParameter("op");
		if ("showList".equals(op)) {
			doShowList(request,response);
		}else if ("playListShow".equals(op)) {
			doPlayListShow(request,response);
		}else if ("getSongDetail".equals(op)) {
			dogetSongDetail(request,response);
		}
	}
	private void dogetSongDetail(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String songId = request.getParameter("songId");;
		Song songBySongId = songService.getSongBySongId(Integer.parseInt(songId));
		String json = gson.toJson(songBySongId);
		response.getWriter().write(json);
	}
	private void doPlayListShow(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String accountId = request.getParameter("accountId");
		List<Song> playListByAccountId = playListService.getPlayListByAccountId(accountId);
		String json = gson.toJson(playListByAccountId);
		response.getWriter().write(json);
	}
	private void doShowList(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, String> map = new HashMap<>();
		String rankingCondition = request.getParameter("rankingCondition");
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");
		map.put("pageNum", pageNum);
		map.put("pageSize", pageSize);
		if (rankingCondition.equals("新歌榜")) {
			map.put("rankingCondition", "");
			map.put("order", "orderByPublishDate");
		}else if (rankingCondition.equals("热歌榜")) {
			map.put("rankingCondition", "");
			map.put("order", "orderByClickCount");
		}else {
			rankingCondition = rankingCondition.substring(0, 2);
			map.put("rankingCondition", rankingCondition);
		}
		PageInfo<Song> songByConditionAndOrder = songService.getSongByConditionAndOrder(map);
		String json = gson.toJson(songByConditionAndOrder);
		response.getWriter().write(json);
	}

}
